package api

import (
	"context"
	"myproject/backend/types"
	"myproject/backend/utils"
)

type OrderApi struct {
	Ctx context.Context
}

func NewOrderApi() *OrderApi {
	return &OrderApi{}
}

func (a *OrderApi) GetOrderLog(req types.GetOrderLogReq) types.OrderLogListResp {
	resp := types.OrderLogListResp{}
	query := map[string]any{}
	if req.OrganId > 0 {
		query["organId"] = req.OrganId
	}
	query["orderId"] = req.OrderId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/customer/glassesStore/orderLog/getOrderLog",
		Resp:        &resp,
		QueryParams: query,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) CreateReturnOrder(req types.CreateReturnOrderReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["organId"] = req.OrganId
	body["item"] = req.Items
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/returnOrder/createReturnOrder",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) GetReturnOrderDetail(req types.GetReturnOrderDetailReqForApi) types.GetReturnOrderDetailRes {
	resp := types.GetReturnOrderDetailRes{}
	query := map[string]any{}
	query["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/customer/glassesStore/returnOrder/getReturnOrderDetail",
		Resp:        &resp,
		QueryParams: query,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 退货列表
func (a *OrderApi) GetReturnOrderList(req types.GetReturnOrderListReqForApi) types.ReturnOrderListResp {
	resp := types.ReturnOrderListResp{}
	query := map[string]any{}
	if req.OrganId > 0 {
		query["organId"] = req.OrganId
	}
	if req.ReturnType > 0 {
		query["returnType"] = req.ReturnType
	}
	if req.CustomerId > 0 {
		query["customerId"] = req.CustomerId
	}
	if req.OldOrderId > 0 {
		query["oldOrderId"] = req.OldOrderId
	}
	if req.StartDate > 0 {
		query["startDate"] = req.StartDate
	}
	if req.EndDate > 0 {
		query["endDate"] = req.EndDate
	}
	if req.ReturnOrderCode != "" {
		query["returnOrderCode"] = req.ReturnOrderCode
	}
	query["page"] = req.Page
	query["pageSize"] = req.PageSize
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/customer/glassesStore/returnOrder/getReturnOrderList",
		QueryParams: query,
		Resp:        &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 废弃订单
func (a *OrderApi) DeleteOrderById(req types.DeleteOrderReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/deleteOrderById",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 创建销售订单
func (a *OrderApi) CreateSaleOrder(req types.CreateOrderReqForApi) types.CreateOrderResqForApiResp {
	resp := types.CreateOrderResqForApiResp{}
	body := map[string]any{}
	body["organId"] = req.OrganId
	body["orderItems"] = req.OrderItems
	body["customerId"] = req.CustomerId
	body["totalAmount"] = req.TotalAmount
	body["paidAmount"] = req.PaidAmount
	body["discount"] = req.Discount
	body["quantity"] = req.Quantity
	body["remark"] = req.Remark
	body["isProcess"] = req.IsProcess
	body["checkListId"] = req.CheckListId
	body["checkCode"] = req.CheckCode
	body["businessUserId"] = req.BusinessUserId
	body["businessUserName"] = req.BusinessUserName
	body["businessTime"] = req.BusinessTime
	body["vipDiscount"] = req.VipDiscount
	body["subTotalDiscount"] = req.SubTotalDiscount
	body["receivableAmount"] = req.ReceivableAmount
	body["totalDiscount"] = req.TotalDiscount
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/createOrder",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 创建未完成眼镜店订单
func (a *OrderApi) CreateGlassesStoreUnfinishedOrder(req types.CreateGlassesStoreUnfinishedOrderReqForApi) types.CreateOrderResqForApiResp {
	resp := types.CreateOrderResqForApiResp{}
	body := map[string]any{}
	body["organId"] = req.OrganId
	body["discountCouponId"] = req.DiscountCouponId
	body["orderItems"] = req.OrderItems
	body["customerId"] = req.CustomerId
	body["totalAmount"] = req.TotalAmount
	body["paidAmount"] = req.PaidAmount
	body["discount"] = req.Discount
	body["quantity"] = req.Quantity
	body["remark"] = req.Remark
	body["isProcess"] = req.IsProcess
	body["checkListId"] = req.CheckListId
	body["checkCode"] = req.CheckCode
	body["businessUserId"] = req.BusinessUserId
	body["businessUserName"] = req.BusinessUserName
	body["businessTime"] = req.BusinessTime
	body["vipDiscount"] = req.VipDiscount
	body["subTotalDiscount"] = req.SubTotalDiscount
	body["receivableAmount"] = req.ReceivableAmount
	body["totalDiscount"] = req.TotalDiscount
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/createGlassesStoreUnfinishedOrder",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// UpdateCustomerTrainCount
func (a *OrderApi) UpdateCustomerTrainCount(req types.UpdateCustomerTrainCountReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["customerId"] = req.CustomerId
	body["trainCount"] = req.TrainCount
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/relation/UpdateCustomerTrainCount",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) CreateGlassesStorePurchaseOrder(req types.CreateGlassesStorePurchaseOrderReqForApi) types.CreateGlassesStorePurchaseOrderRespForApiResp {
	resp := types.CreateGlassesStorePurchaseOrderRespForApiResp{}
	body := map[string]any{}
	body["glassesStorePurchaseOrder"] = req.GlassesStorePurchaseOrder
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/createGlassesStorePurchaseOrder",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) GetGlassesStorePurchaseOrderDetail(req types.GetGlassesStorePurchaseOrderDetailReqForApi) types.GetGlassesStorePurchaseOrderDetailRespForApiResp {
	resp := types.GetGlassesStorePurchaseOrderDetailRespForApiResp{}
	body := map[string]any{}
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/getGlassesStorePurchaseOrderDetail",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) GetGlassesStorePurchaseOrderList(req types.GetGlassesStorePurchaseOrderListReqForApi) types.GetGlassesStorePurchaseOrderListRespForApiResp {
	body := map[string]any{}
	if req.Status > 0 {
		body["status"] = req.Status
	}
	if req.ProviderInfo != "" {
		body["providerInfo"] = req.ProviderInfo
	}
	if req.StartDate > 0 {
		body["startDate"] = req.StartDate
	}
	if req.EndDate > 0 {
		body["endDate"] = req.EndDate
	}
	if req.OrderCode != "" {
		body["orderCode"] = req.OrderCode
	}
	if req.ProviderId > 0 {
		body["providerId"] = req.ProviderId
	}
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	body["page"] = req.Page
	body["pageSize"] = req.PageSize
	resp := types.GetGlassesStorePurchaseOrderListRespForApiResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/getGlassesStorePurchaseOrderList",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) UpdateGlassesStorePurchaseOrder(req types.UpdateGlassesStorePurchaseOrderReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["glassesStorePurchaseOrder"] = req.GlassesStorePurchaseOrder
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/updateGlassesStorePurchaseOrder",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) DeleteGlassesStorePurchaseOrder(req types.DeleteGlassesStorePurchaseOrderReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/deleteGlassesStorePurchaseOrder",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// post /getGlassesStorePurchaseOrderRecordList (GlassesStorePurchaseOrderRecordListReqForApi) returns (GlassesStorePurchaseOrderRecordListRespForApi)
func (a *OrderApi) GetGlassesStorePurchaseOrderRecordList(req types.GlassesStorePurchaseOrderRecordListReqForApi) types.GlassesStorePurchaseOrderRecordListRespForApiResp {
	resp := types.GlassesStorePurchaseOrderRecordListRespForApiResp{}
	body := map[string]any{}
	body["orderId"] = req.OrderId
	body["page"] = req.Page
	body["size"] = req.Size
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/getGlassesStorePurchaseOrderRecordList",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 废弃订单
// /customer/glassesStore/order/deleteOrderById (DeleteOrderReq) returns (JsResp)
func (a *OrderApi) CancelGlassesStorePurchaseOrder(req types.CancelGlassesStorePurchaseOrderReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["orderId"] = req.OrderId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/cancelGlassesStorePurchaseOrder",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// @doc "确认眼镜店采购单"
// @handler ConfirmGlassesStorePurchaseOrder
// post /confirmGlassesStorePurchaseOrder (ConfirmGlassesStorePurchaseOrderReqForApi) returns (EmptyResp)
func (a *OrderApi) ConfirmGlassesStorePurchaseOrder(req types.ConfirmGlassesStorePurchaseOrderReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	body["status"] = req.Status
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/confirmGlassesStorePurchaseOrder",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 售后服务
// 查询售后服务详情
func (a *OrderApi) GetAfterSaleDetail(req types.GetAfterSaleDetailReq) types.GetAfterSaleDetailListResp {
	resp := types.GetAfterSaleDetailListResp{}
	body := map[string]any{}
	body["orderCode"] = req.OrderCode
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/sales/after/sale/process/detail",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) GetAfterSaleDetailByAfterSaleId(req types.GetAfterSaleDetailReq) types.GetAfterSaleDetailInfoResp {
	resp := types.GetAfterSaleDetailInfoResp{}
	body := map[string]any{}
	body["orderCode"] = req.OrderCode
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/sales/after/sale/detail",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// 提交售后服务
func (a *OrderApi) ApplyAfterSale(req types.ApplyAfterSaleReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["categoryName"] = req.CategoryName
	body["contact"] = req.Contact
	body["content"] = req.Content
	body["outCode"] = req.OutCode
	body["phone"] = req.Phone
	body["problemRecord"] = req.ProblemRecord
	body["title"] = req.Title
	body["typeName"] = req.TypeName
	if len(req.Images) > 0 {
		body["images"] = req.Images
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/sales/after/sale/apply",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// /sales/after/sale/list
func (a *OrderApi) GetAfterSaleList(req types.GetAfterSaleListReq) types.GetAfterSaleListResp {
	resp := types.GetAfterSaleListResp{}
	body := map[string]any{}
	body["typeId"] = req.TypeId
	body["page"] = req.Page
	body["pageSize"] = req.PageSize
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/sales/after/sale/list",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

// /customer/glassesStore/order/getStatementsList
func (a *OrderApi) GetStatementsList(req types.GetStatementsListReqForBranchReq) types.GetStatementsListReqForApiResp {
	resp := types.GetStatementsListReqForApiResp{}
	body := map[string]any{}
	body["organIds"] = req.OrganIds
	body["organQueries"] = req.OrganQueries
	body["orderCode"] = req.OrderCode
	body["page"] = req.Page
	body["size"] = req.Size
	body["bType"] = req.BType
	body["orderType"] = req.OrderType
	body["startDate"] = req.StartDate
	body["endDate"] = req.EndDate
	body["createName"] = req.CreateName
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/getStatementsList",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}

func (a *OrderApi) DeleteStatements(req types.DeleteStatementsReqForApi) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["ids"] = req.Ids
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/deleteStatements",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}
func (a *OrderApi) CreateStatements(req types.CreateStatementsReqForApi) types.CreateStatementsRespForApiResp {
	resp := types.CreateStatementsRespForApiResp{}
	body := map[string]any{}
	body["statement"] = req.Statement
	if req.OrganId > 0 {
		body["organId"] = req.OrganId
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/customer/glassesStore/order/createStatements",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
	}
	return resp
}
